package com.mk.plugin.function;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

public class PageFunction extends Functions {
    public PageFunction(String fieldName) {
        super(fieldName);
    }

    @Override
    public List<String> getNeedComponent() {
        return Arrays.asList("Pagination");
    }

    @Override
    public List<String> getSubviewProps() {
        return Arrays.asList(
                "pages"
        );
    }

    @Override
    public String getSubviewStatement() {
        return "onPageItemClick(index : number, pageSize : number) : void {\n" +
                "            const { onChangePageOrPageSize } = this.props;\n" +
                "            onChangePageOrPageSize( index, pageSize);\n" +
                "        }";
    }

    @Override
    public String getActionString(Map<String, Object> properties) {
        return String.format("export function onChangePageOrPageSize(page, pageSize) {\n" +
                "    return {\n" +
                "        type : ActionType.ON_CHANGE_PAGE_OR_PAGE_SIZE_FOR_%s,\n" +
                "        page,\n" +
                "        pageSize\n" +
                "    }\n" +
                "}\n" +
                "\n", properties.get("allUpperCaseUnderscoreName"));
    }

    @Override
    public String getActionTypeString(Map<String, Object> properties) {
        return String.format("export const ON_CHANGE_PAGE_OR_PAGE_SIZE_FOR_%s = \"ON_CHANGE_PAGE_OR_PAGE_SIZE_FOR_%s\";\n", properties.get("allUpperCaseUnderscoreName"), properties.get("allUpperCaseUnderscoreName"));
    }

    @Override
    public String getReducerSwitches(Map<String, Object> properties) {
        return String.format("case ActionType.ON_CHANGE_PAGE_OR_PAGE_SIZE_FOR_%s:\n" +
                "            return handleChangePageOrPageSizeFor%s(state, action);\n",
                properties.get("allUpperCaseUnderscoreName"), properties.get("UpperPageName"));
    }

    @Override
    public String getReducerFunctions(Map<String, Object> properties) {
        return String.format("function handleChangePageOrPageSizeFor%s(state, action) {\n" +
                "    let searchCondition = Object.assign({}, state.searchCondition, {\n" +
                "        page     : action.page,\n" +
                "        pageSize : action.pageSize\n" +
                "    });\n" +
                "    return Object.assign({}, state, {\n" +
                "         searchCondition\n" +
                "    })\n" +
                "}\n" +
                "\n", properties.get("UpperPageName"));
    }

    @Override
    public List<String> getSearchConditionInitState(Map<String, Object> properties) {
        return Arrays.asList(
                "page : 0",
                String.format("pageSize : MKPagination.getCachePageSize(\"pagination%s\")", properties.get("UpperPageName"))
        );
    }

    @Override
    public List<String> getMKComponentImport() {
        return Arrays.asList(
                "MKPagination",
                "MKPaginatorDelegate"
        );
    }

    @Override
    public List<String> getReducerImportStatement() {
        return Arrays.asList("import {MKPagination} from \"mk-component\";");
    }
}
